package com.das.mechanic_base.adapter.login;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.login.AreaBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3AreaAdapter extends RecyclerView.Adapter<X3AreaAdapter.AreaHolder> {

    private Context mContext;

    private List<AreaBean> mList;

    private List<Boolean> bList;

    public X3AreaAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        this.bList = new ArrayList<>();
    }

    public void changeData(List<AreaBean> mList, String select) {
        this.mList = mList;
        this.bList.clear();
        for (int i = 0; i < mList.size(); i++) {
            bList.add(select.equals(mList.get(i).getValue()));
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AreaHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AreaHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_area_select_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull AreaHolder holder, int position) {
//        if (bList.get(position)) {
//            holder.itemView.setBackgroundColor(Color.parseColor("#f5f5f5"));
//        }else {
//            holder.itemView.setBackgroundColor(0);
//        }

        String imgUrl = "";
        AreaBean.KwParamBean kwParam1 = mList.get(position).getKwParam();
        if (kwParam1 != null) {
            imgUrl = kwParam1.getResourceUrl();
        }
        X3GlideNewUtils.loadNormalImage(mContext, X3StringUtils.getImageUrl(imgUrl),holder.iv_icon,R.mipmap.x3_car_img_bg);
        holder.tv_area.setText(mList.get(position).getValue());

        holder.itemView.setOnClickListener(v -> {
            if (iOnClickArea != null) {
                String url = "";
                String localUrl = "";
                String localH5Url = "";
                AreaBean.KwParamBean kwParam = mList.get(position).getKwParam();
                if (kwParam != null) {
                    url = kwParam.getResourceUrl();
                    localUrl = kwParam.getLocalUrl();
                    localH5Url = kwParam.getH5LocalUrl();
                }
                iOnClickArea.iOnClickAreaSelect(mList.get(position).getValue(),url,localUrl,localH5Url);
            }
        });

    }

    IOnClickArea iOnClickArea;

    public void setiOnClickArea(IOnClickArea iOnClickArea) {
        this.iOnClickArea = iOnClickArea;
    }

    public interface IOnClickArea {
        void iOnClickAreaSelect(String area,String imgUrl,String localUrl,String localH5Url);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class AreaHolder extends RecyclerView.ViewHolder {
        TextView tv_area;
        ImageView iv_icon;
        public AreaHolder(@NonNull View itemView) {
            super(itemView);
            tv_area = itemView.findViewById(R.id.tv_area);
            iv_icon = itemView.findViewById(R.id.iv_icon);
        }
    }
}

